<?php
namespace app\console\controller;
use app\console\controller\Auth;
use think\facade\Config;
use think\Db;
use app\service\controller\File;

/**
 * 首页设置
 * */
class Hot extends Auth
{
    // 热门菜品设置
    public function con_list(){
        $lists=Db::table('tp_hot')
            ->paginate(10);
        $this->assign('active','hot');
        $this->assign([
            'topbar' => ['人气菜品设置', '热门菜品设置'],
            'seting' => Config::pull('cms_config'),
            'lists'  =>$lists
        ]);
        return $this->fetch();
    }

    //热门菜品添加
    public function con_add(){
        if ($this->request->isAjax()) {
            $data = $this->request->post();
            //上传图片
            $cate_img = (new \app\service\controller\File())->upload_single_img('head_img', 'head_img');
            if (is_array($cate_img)) {
                if ($cate_img['code'] == 1) {
                    $data['head_img'] = $cate_img['img_path'];
                } else {
                    $this->error($cate_img['msg']);
                }
            } else {
                unset($data['head_img']);
            }
            //更新栏目
                // 3是卤菜加盟
            $data['navigation_id']=2;
            $res=Db::table('tp_hot')->insert($data);
            $this->success('新增成功');
            exit;
        }
        $this->assign([
            'topbar' => ['人气菜品设置', '热门菜品设置','添加'],
        ]);
        return $this->fetch();
    }
    //行业前景修改
    public function con_edit($id){
        if ($this->request->isAjax()) {
            $data = $this->request->post();
            //上传图片
            $cate_img = (new \app\service\controller\File())->upload_single_img('head_img', 'head_img');
            if (is_array($cate_img)) {
                if ($cate_img['code'] == 1) {
                    $data['head_img'] = $cate_img['img_path'];
                } else {
                    $this->error($cate_img['msg']);
                }
            } else {
                unset($data['head_img']);
            }
            //更新栏目
            $res=Db::table('tp_hot')->where('id',$id)->update($data);
            $this->success('修改成功');
            exit;
        }
        $res=Db::table('tp_hot')->where('id',$id)->find();
        $this->assign([
            'topbar' => ['人气菜品设置', '热门菜品设置','修改'],
            'seting' => Config::pull('cms_config'),
            'list'   =>$res
        ]);
        return $this->fetch();
    }
    //导航加载
    public function ml_list(){
        $lists=Db::table('tp_series')->paginate(15);
        $this->assign('active', 'hot');
        $this->assign([
            'topbar' => ['人气菜品设置', '麻辣鲜香导航设置'],
            'seting' => Config::pull('cms_config'),
            'lists'  =>$lists
        ]);
        return $this->fetch();
    }
    //导航修改
    public function ml_edit($id){
        if ($this->request->isAjax()) {
            $data = $this->request->post();
            //更新栏目
            $res=Db::table('tp_series')->where('id',$id)->update($data);
            $this->success('修改成功');
            exit;
        }
        $res=Db::table('tp_series')->where('id',$id)->find();

        $this->assign([
            'topbar' => ['人气菜品设置', '麻辣鲜香导航设置','修改'],
            'seting' => Config::pull('cms_config'),
            'list'   =>$res
        ]);
        return $this->fetch();
    }

    // 麻辣鲜香详情设置
    public function text_list(){
        $lists=Db::table('tp_series_par')
            ->leftJoin('tp_series','tp_series.id=tp_series_par.series_id')
            ->field('tp_series.title,tp_series_par.*')
            ->where('sale',1)
            ->paginate(10);
        $this->assign('active','hot');
        $this->assign([
            'topbar' => ['人气菜品设置', '麻辣鲜香详情设置'],
            'seting' => Config::pull('cms_config'),
            'lists'  =>$lists
        ]);
        return $this->fetch();
    }
    //麻辣鲜香详情添加
    public function text_add(){
        if ($this->request->isAjax()) {
            $data = $this->request->post();
            //上传图片
            $cate_img = (new \app\service\controller\File())->upload_single_img('head_img', 'head_img');
            if (is_array($cate_img)) {
                if ($cate_img['code'] == 1) {
                    $data['head_img'] = $cate_img['img_path'];
                } else {
                    $this->error($cate_img['msg']);
                }
            } else {
                unset($data['head_img']);
            }
            //更新栏目
            $data['sale']=1;
            $res=Db::table('tp_series_par')->insert($data);
            $this->success('新增成功');
            exit;
        }
        $list=Db::table('tp_series')->select();
        $this->assign([
            'topbar' => ['人气菜品设置', '麻辣鲜香详情设置','添加'],
            'list'=>$list
        ]);
        return $this->fetch();
    }
    //麻辣鲜香详情修改
    public function text_edit($id){
        if ($this->request->isAjax()) {
            $data = $this->request->post();
            //上传图片
            $cate_img = (new \app\service\controller\File())->upload_single_img('head_img', 'head_img');
            if (is_array($cate_img)) {
                if ($cate_img['code'] == 1) {
                    $data['head_img'] = $cate_img['img_path'];
                } else {
                    $this->error($cate_img['msg']);
                }
            } else {
                unset($data['head_img']);
            }
            //更新栏目

            $res=Db::table('tp_series_par')->where('id',$id)->update($data);
            $this->success('修改成功');
            exit;
        }
        //$res=Db::table('tp_series_par')->where('id',$id)->find();
        $res=Db::table('tp_series_par')
            ->leftJoin('tp_series','tp_series.id=tp_series_par.series_id')
            ->field('tp_series.title,tp_series.id as pid,tp_series_par.*')
            ->where('tp_series_par.id',$id)->find();
        $series=Db::table('tp_series')->select();
        $this->assign([
            'topbar' => ['人气菜品设置', '热门菜品设置','修改'],
            'seting' => Config::pull('cms_config'),
            'list'   =>$res,
            'series' =>$series
        ]);
        return $this->fetch();
    }
}
